<template>
  <div class="type-nav">
    <div class="container">
      <div @mouseenter="isotherIn = true" @mouseleave="isotherIn = false">
        <h2 class="all">全部商品分类</h2>
        <transition name="nav">
          <div
            class="sort"
            v-show="isShowNav || isotherIn"
            @click="navToSearch"
          >
            <div
              class="all-sort-list2"
              @mouseleave=";(isShowInde = -1), (isEnter = false)"
              @mouseenter="isEnter = true"
            >
              <div
                class="item"
                v-for="(category1, index) of CatGorylist"
                :key="category1.categoryId"
                :class="isShowInde === index && isEnter ? 'active' : ''"
                @mouseenter="orificing(index)"
              >
                <h3>
                  <a
                    href="javascript:;"
                    :data-category-name="category1.categoryName"
                    :data-category1-id="category1.categoryId"
                    >{{ category1.categoryName }}</a
                  >
                </h3>
                <div class="item-list clearfix">
                  <div class="subitem">
                    <dl
                      class="fore"
                      v-for="category2 in category1.categoryChild"
                      :key="category2.categoryId"
                    >
                      <dt>
                        <a
                          href="javascript:;"
                          :data-category-name="category2.categoryName"
                          :data-category2-id="category2.categoryId"
                          >{{ category2.categoryName }}</a
                        >
                      </dt>
                      <dd>
                        <em
                          v-for="category3 in category2.categoryChild"
                          :key="category3.categoryId"
                        >
                          <a
                            href="javascript:;"
                            :data-category-name="category3.categoryName"
                            :data-category3-id="category3.categoryId"
                            >{{ category3.categoryName }}</a
                          >
                        </em>
                      </dd>
                    </dl>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </transition>
      </div>
      <nav class="nav">
        <a href="###">服装城</a>
        <a href="###">美妆馆</a>
        <a href="###">尚品汇超市</a>
        <a href="###">全球购</a>
        <a href="###">闪购</a>
        <a href="###">团购</a>
        <a href="###">有趣</a>
        <a href="###">秒杀</a>
      </nav>
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { throttle } from 'lodash'
export default {
  name: 'TypeNav',
  data() {
    return {
      isShowInde: -1,
      isEnter: false,
      isotherIn: false,
    }
  },

  mounted() {
    // 生成节流函数
    this.orificing = throttle(this.orificingCentent, 100)
  },
  computed: {
    ...mapState('category', ['CatGorylist']),
    isShowNav() {
      // 判断,当当前路由内的name改变时判断是否还是为name',如果不是,就会隐藏Nav
      return this.$route.name === 'home'
      // 但是,在其他页面当移入时,也需要使用移入移出判断是否显示
      // return
    },
  },
  watch: {
    $route() {
      // 每次跳转时都需要隐藏下拉列表
      this.isotherIn = false
    },
  },
  methods: {
    // 手动创建的节流函数无法在最后在执行一次,只有移入移出才能执行,所以使用外部lodash中的函数
    // orificingMake() {
    //   let lastTime = 0
    //   return function (index) {
    //     let nowTime = Date.now()
    //     if (nowTime - lastTime < 500) return
    //     lastTime = nowTime
    //     this.isShowInde = index
    //   }
    // },
    orificingCentent(index) {
      this.isShowInde = index
    },
    // 但是节流函数生成后会有一个弊端,就是当快速移出时会导致最后一个移出框最后触发一次执行,变成选中状态
    orificing() {},
    // 编程式路由跳转,跳转时携带参数供search使用
    // 为每一个a绑定事件的话太浪费性能,所以使用事件委派形式
    // 使用自定义属性传递参数
    navToSearch(e) {
      if (e.target.nodeName !== 'A') return
      let { categoryName, category1Id, category2Id, category3Id } =
        e.target.dataset
      this.$router.push({
        name: 'search',
        params: this.$route.params,
        query: {
          categoryName,
          category1Id,
          category2Id,
          category3Id,
        },
      })
    },
  },
}
</script>

<style lang="less" scoped>
.nav-enter-active,
.nav-leave-active {
  transition: all 0.2s linear;
}
.nav-enter,
.nav-leave-to {
  opacity: 0;
}
.type-nav {
  border-bottom: 2px solid #e1251b;

  .container {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    position: relative;

    .all {
      width: 210px;
      height: 45px;
      background-color: #e1251b;
      line-height: 45px;
      text-align: center;
      color: #fff;
      font-size: 14px;
      font-weight: bold;
    }

    .nav {
      a {
        height: 45px;
        margin: 0 22px;
        line-height: 45px;
        font-size: 16px;
        color: #333;
      }
    }

    .sort {
      position: absolute;
      left: 0;
      top: 45px;
      width: 210px;
      height: 461px;
      position: absolute;
      background: #fafafa;
      z-index: 999;
      & .active {
        background-color: red;
        color: #fff;
        .item-list {
          display: block !important;
        }
      }
      .all-sort-list2 {
        .item {
          h3 {
            a {
              line-height: 30px;
              font-size: 14px;
              font-weight: 400;
              overflow: hidden;
              padding: 0 20px;
              margin: 0;
              display: block;
              color: #333;
              &:hover {
                text-decoration: none;
                color: black !important;
              }
            }
          }

          .item-list {
            display: none;
            position: absolute;
            width: 734px;
            min-height: 460px;
            background: #f7f7f7;
            left: 210px;
            border: 1px solid #ddd;
            top: 0;
            z-index: 9999 !important;

            .subitem {
              float: left;
              width: 650px;
              padding: 0 4px 0 8px;

              dl {
                border-top: 1px solid #eee;
                padding: 6px 0;
                overflow: hidden;
                zoom: 1;

                &.fore {
                  border-top: 0;
                }

                dt {
                  float: left;
                  width: 54px;
                  line-height: 22px;
                  text-align: right;
                  padding: 3px 6px 0 0;
                  font-weight: 700;
                }

                dd {
                  float: left;
                  width: 415px;
                  padding: 3px 0 0;
                  overflow: hidden;

                  em {
                    float: left;
                    height: 14px;
                    line-height: 14px;
                    padding: 0 8px;
                    margin-top: 5px;
                    border-left: 1px solid #ccc;
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
</style>
